﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.ApplicationModel;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.ViewManagement;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

namespace WellDone
{
    public sealed partial class ServiceAddressDialog : Page
    {
        public event EventHandler CloseRequested;
        public bool bUpdated = false;
        public string strServiceAddress;

        public ServiceAddressDialog()
        {
            this.InitializeComponent();
        }
        public void init()
        {
            string strClientAddress;
            strServiceAddress = Globals.client.Endpoint.Address.Uri.ToString();
            // Determine if an address has been set...
            var localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
            Object value = localSettings.Values[Globals.k_str_settings_service_address];
            if (value != null)
            {
                strClientAddress = value.ToString().Trim();
                if (strClientAddress.Length > 0) // Assume this is a good address
                    strServiceAddress = strClientAddress;
            }

            // Show version information
            var version = Package.Current.Id.Version;
            string appVersion = "Version " + String.Format("{0}.{1}.{2}.{3}", version.Major, version.Minor, version.Build, version.Revision);
            tblkVersion.Text = appVersion;

            // Show current web service address...
            tbServiceAddress.Text = strServiceAddress;
            // Get focus...
            var pane = InputPane.GetForCurrentView();
            pane.Showing += pane_Showing;
            pane.Hiding += pane_Hiding;
        }
        private void pane_Showing(InputPane sender, InputPaneVisibilityEventArgs e)
        {
            // Shift the dialog up...
            this.RootPanel.Margin = new Thickness(0, -1 * e.OccludedRect.Y, 0, 0);
        }
        private void pane_Hiding(InputPane sender, InputPaneVisibilityEventArgs e)
        {
            // Restore...
            this.RootPanel.Margin = new Thickness(0, 0, 0, 0);
        }
        private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            // Update information...
            bUpdated = true;
            strServiceAddress = tbServiceAddress.Text;
            // Close the dialog
            if (this.CloseRequested != null)
            {
                this.CloseRequested(this, EventArgs.Empty);
            }
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            // Close the dialog
            if (this.CloseRequested != null)
            {
                this.CloseRequested(this, EventArgs.Empty);
            }
        }
    }
}
